package com.lagou.task16;

import java.io.IOException;

public class ExceptionPreventTest {
    public static void main(String[] args) {
        //算数异常
        int ia = 10;
        int ib = 0;
        if (ib != 0){
            System.out.println(ia /ib);//java.lang.ArithmeticException
        }


        //数组下标越界异常
        int[] arr = new int[5];
        int pos = 5;
        if (pos >= 5) pos = 4;
        System.out.println(arr[pos]);//java.lang.ArrayIndexOutOfBoundsException
        //空指针异常
        String str =null;
        if (str == null) str = "null";
        System.out.println(str.length());//java.lang.NullPointerException

        //类型转换异常
        Exception ex = new Exception();
        if (ex instanceof IOException) {
            IOException ie = (IOException)ex;//java.lang.ClassCastException
        }


        //数字格式异常
        String str2 = "123a";
        if (str2.matches("\\d+")){
            System.out.println(Integer.parseInt(str2));//java.lang.NumberFormatException
        }

        System.out.println("程序正常结束了");

    }
}
